package com.wbao.dianniu.ui;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.im.LoginSampleHelper;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.GlobalUserData;
import com.wbao.dianniu.logical.SharedPreferencesUtils;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.update.UpdateManager;
import com.wbao.dianniu.update.UpdateManager.ICheckCompletedListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.PermissionUtils;

/**
 * 设置
 *
 * @author yejianjun
 */
public class SettingActivity extends BaseActivity implements ICheckCompletedListener {

    public RelativeLayout rl_account, rl_privacy, rl_base, rl_about, rl_exist;
    private RelativeLayout rl_feedback;
    AlertDialog alertDialog;
    private RelativeLayout rl_checkUpdate;
    private RelativeLayout rl_invite;
    private RelativeLayout rl_share;
    private Dialog pDialog;
    private UpdateManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_setting, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.setting));
        init();
        manager = new UpdateManager(this);
        manager.addCheckListener(this);
        GlobalUserData userData = new GlobalUserData();
        LoginSampleHelper.getInstance().getInfos(userData.getEnvironemt() + GlobalContext.getAccountId());
    }

    private void init() {
        rl_account = (RelativeLayout) findViewById(R.id.rl_account);
        rl_privacy = (RelativeLayout) findViewById(R.id.rl_privacy);
        rl_base = (RelativeLayout) findViewById(R.id.rl_base);
        rl_about = (RelativeLayout) findViewById(R.id.rl_about);
        rl_feedback = (RelativeLayout) findViewById(R.id.rl_feedback);
        rl_exist = (RelativeLayout) findViewById(R.id.rl_exist);
        rl_checkUpdate = (RelativeLayout) findViewById(R.id.rl_check_update);
        rl_invite = (RelativeLayout) findViewById(R.id.rl_invite_friends);
        rl_share = (RelativeLayout) findViewById(R.id.rl_share_to_friends);

        rl_account.setOnClickListener(this);
        rl_privacy.setOnClickListener(this);
        rl_base.setOnClickListener(this);
        rl_about.setOnClickListener(this);
        rl_feedback.setOnClickListener(this);
        rl_exist.setOnClickListener(this);
        rl_checkUpdate.setOnClickListener(this);
        rl_invite.setOnClickListener(this);
        rl_share.setOnClickListener(this);

    }

    private void showWaittingDialog() {
        manager.checkUpdate();
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.show();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_account:
                intent(AccountInfoActivity.class);
                break;
            case R.id.rl_privacy:
                intent(PrivacySettingActivity.class);
                break;
            case R.id.rl_base:
                intent(BaseSetting.class);
                break;
            case R.id.rl_about:
                Intent aboutIntent = new Intent();
                aboutIntent.setClass(this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
            case R.id.rl_check_update:
                showWaittingDialog();
                break;
            case R.id.rl_invite_friends:// 邀请好友加入电纽
                PermissionUtils.getInstance().intentToContact(this);
                break;
            case R.id.rl_share_to_friends:// 分享给好友
                ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                shareSDKManager.showShare(this, "加入电纽，身价百倍！https://www.dianniu.me/down/index.html", 3, GlobalContext.cdndownUrl + "logo.png", GlobalContext.getAccountId());
                break;
            case R.id.rl_feedback:
                Intent feedIntent = new Intent();
                feedIntent.setClass(this, FeedbackActivity.class);
                startActivity(feedIntent);
                break;
            case R.id.rl_exist:
                CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle("确认退出吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        SharedPreferencesUtils.init(SettingActivity.this);
                        SharedPreferencesUtils.setAutoLoginStatus(false);
                        //设置你的操作事项
                        LoginSampleHelper.getInstance().loginOut_Sample();
                        LoginSampleHelper.getInstance().setIMKit(null);
                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        overridePendingTransition(0, R.anim.push_bottom_out);
                        SettingActivity.this.finish();
                    }
                });
                builder.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeCheckListener();
        }
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

    }

    private void intent(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(SettingActivity.this, cls);
        startActivity(intent);
    }

    @Override
    public void checkCompleted() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    public void noCheck() {
        Notification.toast(this, "当前版本已是最新版本");
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    public void noPermission() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        PermissionUtils.getInstance().showMsg(this, "更新失败，没有读取权限，\n请点击\"设置\"-\"权限管理\"-打开所有权限");
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
